package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.WorkDao;
import com.fuli.app.dto.MessageDTO;
import com.fuli.app.dto.WorkDTO;
import com.fuli.app.dto.query.WorkQueryDTO;
import com.fuli.app.entity.Message;
import com.fuli.app.entity.Work;
import com.fuli.app.service.WorkService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    private WorkDao workDao;
    @Value("${upload.image.path}")
    private String imagePath;
    @Value("${upload.image.dir}")
    private String imageDir;
    @Value("${upload.image.trash}")
    private String imageTrash;

    @Override
    public ActionResult<WorkDTO> add(MultipartFile file, String curriculumId, String content, String exerciseId, String userId) {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf('.'));
        Work po = EntityUtils.init(Work.class);
        String imageName = po.getId() + suffix;
        po.setName(filename);
        po.setUrl(imagePath + imageName);
        po.setRealPath(imageDir + imageName);
        po.setCurriculumId(curriculumId);
        po.setContent(content);
        po.setExerciseId(exerciseId);
        po.setUserId(userId);
        po.setDealed(false);
        File dest = new File(po.getRealPath());
        //判断目录是否存在
        if (!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            System.out.println("保存图片异常");
            e.printStackTrace();
        }
        workDao.insert(po);
        return new ActionResult<>(EntityUtils.copyObject(po, WorkDTO.class));
    }

    @Override
    public ActionResult<String> delete(String id) {
        Work po = workDao.get(id);
        if (po == null)
            return new ActionResult<>(id);
        workDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        workDao.batchDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<WorkDTO> get(String id) {
        Work po = workDao.get(id);
        WorkDTO dto = EntityUtils.copyObject(po, WorkDTO.class);
        return new ActionResult<>(dto);
    }

    @Override
    public ActionResult<Pagination<WorkDTO>> listByQueryDTO(WorkQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = workDao.getCountByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<Work> works = workDao.listByQueryDTO(queryDTO);
            List<WorkDTO> dtos = EntityUtils.copyList(works, WorkDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }

    @Override
    public ActionResult<String> deal(String id) {
        workDao.deal(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDeal(List<String> ids) {
        workDao.batchDeal(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<String> grade(String id, String grade) {
        Work po = workDao.get(id);
        if (po == null)
            return new ActionResult<>(-1, "id不存在");
        po.setGrade(grade);
        workDao.grade(po);
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<WorkDTO>> getByCurriculumId(String curriculumId) {
        List<Work> po = workDao.getByCurriculumId(curriculumId);
        List<WorkDTO> dto = EntityUtils.copyList(po, WorkDTO.class);
        return new ActionResult<>(dto);
    }

    @Override
    public ActionResult<List<WorkDTO>> getByExerciseId(String exerciseId) {
        List<Work> po = workDao.getByExerciseId(exerciseId);
        List<WorkDTO> dto = EntityUtils.copyList(po, WorkDTO.class);
        return new ActionResult<>(dto);
    }
}
